//登陆状态
function islogin(){
    var result;
    $.ajax({
        url:"/user/islogin.do",
        type: "get",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async:false,
        success: function (jsonData) {
            result=jsonData.result.YHBH;
        },
        error:function (error) {
            result= null;
        }
    });
    return result;
}

$(function () {
    /*页面顶部轮播*/
        var length = $('.slider-main>li').length,
            startIndex = null,
            canAuto = false,
            t = 3000,
            timer = null,
            currentIndex = 0;
        $('.slider-main>li:not(:first)').hide();
        $('.slider-main,.btn,.index').hover(
            function () {
                stop();
            },
            function () {
                start();
            });
        $(".index>li").mouseenter(function () {
            startIndex = currentIndex;
            currentIndex = $(this).index();
            play(startIndex, currentIndex);
        });
        $('.pre').click(function () {
            startIndex = currentIndex;
            currentIndex = --currentIndex % length;
            play(startIndex, currentIndex);
        });
        $('.next').click(function () {
            startIndex = currentIndex;
            currentIndex = ++currentIndex % length;
            play(startIndex, currentIndex);
        });

        function play() {
            if (startIndex === currentIndex) {
                return;
            }
            $('.slider-main>li').eq(startIndex).stop().fadeOut(0).parent().children().eq(currentIndex).stop().fadeIn(0);
            $('.index>li').eq(currentIndex).addClass
            ('active').siblings().removeClass('active');
        }

        function start() {
            if (!canAuto) {
                canAuto = true;
                timer = setInterval(function () {
                    $('.next').triggerHandler('click');
                }, t);
            }
        }

        function stop() {
            canAuto = false;
            clearInterval(timer);
        }

        start();

    var mobile_flag = isMobile(); // true为手机端，false为PC端
    //手机端类型
    if (mobile_flag) {
        $(".main-left .content").css({"width": "720px", "overflow": "hidden"});
        $(".nav .block").css({"margin-left": "2%"});
    }
    //商品购买操作
    $(".reduce").click(function () {
        var Number = parseInt($("#buyNumber").val());
        if (Number - 1 > 0) {
            $("#buyNumber").val(Number - 1);
        }
    });
    $(".plus").click(function () {
        var Number = parseInt($("#buyNumber").val());
        $("#buyNumber").val(Number + 1);
    });
    //导航条操作
    $(".nav  ul li").mouseover(function () {
        $(this).children("ul").css("display", "block");
    });
    $(".nav ul li").mouseleave(function () {
        $(this).children("ul").css("display", "none");
    });


    //进入商品页面
    var url = window.location.href;
    var time = 1;
    if (url.indexOf("Product") > 0) {
        setInterval(function (args) {
            time++;
        }, 1000)

    }

    //加入购物车
    $("#btn_addCart").click(function () {

        //获取登陆状态
        var isogin=islogin();
        //当前为登陆状态，购物车数据保存在服务器
        if(isogin!=null){

        }else{

        }
    });

    //离开当前页面
    $.Close = function () {
        //记录浏览商品
        var url = window.location.href;
        if (url.indexOf("Product") > 0) {
            $.ajax({
                url: "/user/History.json",
                type: "post",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({"json": "1", "LLURL": url, "YHIP": returnCitySN["cip"], "LLSJ": time}),
                dataType: "json",
                success: function (jsonData) {
                    alert(jsonData);
                }
            });
        }
    }
    //$("body").attr("onbeforeunload","$.Close()");
    function isMobile() {
        var userAgentInfo = navigator.userAgent;

        var mobileAgents = ["Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod"];

        var mobile_flag = false;

        //根据userAgent判断是否是手机
        for (var v = 0; v < mobileAgents.length; v++) {
            if (userAgentInfo.indexOf(mobileAgents[v]) > 0) {
                mobile_flag = true;
                break;
            }
        }

        var screen_width = window.screen.width;
        var screen_height = window.screen.height;

        //根据屏幕分辨率判断是否是手机
        if (screen_width < 500 && screen_height < 800) {
            mobile_flag = true;
        }

        return mobile_flag;
    }

});